﻿/*
[/filesystem/filesystem.js]
*/

Ext.namespace("tk","tk.yunos","tk.yunos.app");

Ext.QuickTips.init();

tk.yunos.app.YunosSourceInsight = function(fileDescriptor, config){
	var panel = new tk.yunos.app.YunosSourceInsight.Panel(fileDescriptor, config);
	var winParams = {
		title: tk.yunos.app.YunosSourceInsight.appname,
		icon: fileDescriptor.icon,
		layout: 'fit',
		items: [
		    panel
		],
		maximized: true
	};
	tk.yunos.system.GLOBAL_WINDOWMGR.newWindow(winParams).show();
};

tk.yunos.app.YunosSourceInsight.Panel = function(fileDescriptor, config){
	var yunosSourceInsight = this;
	
	var openedFiles = [];
	
	var openFile = function(fileDescriptor){
		if(fileDescriptor && fileDescriptor.mime != 'folder'){
			if(openedFiles[fileDescriptor.address]){
				insightPanel.activate(openedFiles[fileDescriptor.address]);
			}else{
				if(yunosSourceInsight.getWindow && yunosSourceInsight.getWindow()){
					yunosSourceInsight.getWindow().wait();
				}
				tk.yunos.app.YunosSourceInsight.switchFileSystem();
				
				var myMime = tk.yunos.filesystem.DEFAULT_MIME[fileDescriptor.mime] 
					|| tk.yunos.filesystem.DYNAMIC_MIME[fileDescriptor.mime];
				tk.yunos.filesystem.loadApp(myMime.openwith);
				var app = eval(myMime.openwith);
				if(app.Panel){
					openedFiles[fileDescriptor.address] = new app.Panel(fileDescriptor,{
						title: '<img width="15px" height="15px" src="' + fileDescriptor.icon + '"></img>' + fileDescriptor.name,
						tabTip: fileDescriptor.address,
						closable: true,
						listeners: {
							'close': function(){
								delete openedFiles[fileDescriptor.address];
							}
						}
					});
					insightPanel.add(openedFiles[fileDescriptor.address]);
					insightPanel.activate(openedFiles[fileDescriptor.address]);
				}else{
					app.call(app, fileDescriptor);
				}
				
				tk.yunos.app.YunosSourceInsight.restoreFileSystem();
				
				if(yunosSourceInsight.getWindow && yunosSourceInsight.getWindow()){
					yunosSourceInsight.getWindow().finish();
				}
			}
		}
	};
	
	var structTree = new tk.yunos.system.FileMgr.StructTree({
		region: 'west',
		width: 250,
		rootPath: '/',
		onlyFolder: false,
		listeners: {
			'dblclick': function(node){
				var fileDescriptor = node.attributes.fileDescriptor;
				openFile(fileDescriptor);
			}
		}
	});
	//展开节点前修改文件系统的某些参数，切换到本应用内部指定的文件系统，然后再还原
	var originalLoadNodeFunc = structTree.loadNode;
	structTree.un('expandnode', structTree.loadNode);
	structTree.loadNode = function(node){
		tk.yunos.app.YunosSourceInsight.switchFileSystem();
		originalLoadNodeFunc(node);
		tk.yunos.app.YunosSourceInsight.restoreFileSystem();
	};
	structTree.on('expandnode', function(node){
		structTree.loadNode(node);
	});
	
	var insightPanel = new Ext.TabPanel({
		region: 'center',
		enableTabScroll: true,
		listeners: {
			'render': function(){
				tk.yunos.app.YunosSourceInsight.switchFileSystem();
				tk.yunos.filesystem.getFileDescriptor('/index.html', function(success, indexFd){
					if(success){
						openFile(indexFd);
					}
				});
				tk.yunos.app.YunosSourceInsight.restoreFileSystem();
			}
		}
	});
	
	config = config || {};
	config.items = [structTree, insightPanel];
	config.layout = 'border';
	
	Ext.apply(this, config);
	//调用父类构造方法
	tk.yunos.app.YunosSourceInsight.Panel.superclass.constructor.call(this, config);
};

Ext.extend(tk.yunos.app.YunosSourceInsight.Panel, Ext.Panel, {
	border: false
});

tk.yunos.app.YunosSourceInsight.systemFrontRootPath = tk.yunos.filesystem.FRONTEND_ROOT_PATH;
tk.yunos.app.YunosSourceInsight.systemBackRootPath = tk.yunos.filesystem.BACKEND_ROOT_PATH;
tk.yunos.app.YunosSourceInsight.FRONTEND_ROOT_PATH = tk.yunos.filesystem.APP_DIR + '/tk.yunos.app/yunos-source-insight/frontend';
tk.yunos.app.YunosSourceInsight.BACKEND_ROOT_PATH = '.';

tk.yunos.app.YunosSourceInsight.switchFileSystem = function(){
	tk.yunos.filesystem.FRONTEND_ROOT_PATH = tk.yunos.app.YunosSourceInsight.FRONTEND_ROOT_PATH;
	tk.yunos.filesystem.BACKEND_ROOT_PATH = tk.yunos.app.YunosSourceInsight.BACKEND_ROOT_PATH; 
};

tk.yunos.app.YunosSourceInsight.restoreFileSystem = function(){
	tk.yunos.filesystem.FRONTEND_ROOT_PATH = tk.yunos.app.YunosSourceInsight.systemFrontRootPath;
	tk.yunos.filesystem.BACKEND_ROOT_PATH = tk.yunos.app.YunosSourceInsight.systemBackRootPath; 
};

tk.yunos.app.YunosSourceInsight.appname = 'yunos源码查看器';